home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Folder Watching / FW Receiver ƒ / Sources / Windows.c < prev   
Encoding:
C/C++ Source or Header  |  1996-02-16  |  9.0 KB  |  396 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Windows.c
  3.  
  4.     Contains:    Handle application's windows
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.             26/01/96            GS        FW Receiver
  13.                                         Changed size of window.
  14.                                         Changed column widths in list.
  15.                                         Added gWindow global - holds window.
  16.                                         Removed example list contents.
  17.                                         Added AddToListContent() routine to add items to
  18.                                         list.
  19.     
  20.             12/18/95            CW        First release
  21.  
  22. */
  23.  
  24.  
  25. #pragma segment Core
  26.  
  27.  
  28.  
  29. // System Includes
  30.  
  31. #ifndef __TYPES__
  32.     #include <Types.h>
  33. #endif
  34.  
  35. #ifndef __WINDOWS__
  36.     #include <Windows.h>
  37. #endif
  38.  
  39. #ifndef __QUICKDRAW__
  40.     #include <Quickdraw.h>
  41. #endif
  42.  
  43. #ifndef __RESOURCES__
  44.     #include <Resources.h>
  45. #endif
  46.  
  47. #ifndef __FONTS__
  48.     #include <Fonts.h>
  49. #endif
  50.  
  51. #ifndef __ERRORS__
  52.     #include <Errors.h>
  53. #endif
  54.  
  55.  
  56.  
  57.  
  58. // Application Includes
  59.  
  60. #ifndef __BAREBONES__
  61.     #include "BareBones.h"
  62. #endif
  63.  
  64. #ifndef __PROTOTYPES__
  65.     #include "Prototypes.h"
  66. #endif
  67.  
  68.  
  69. // Globals
  70.  
  71. WindowRef    gWindow = NULL;
  72.  
  73.  
  74. // Static prototypes
  75.  
  76. static OSErr    CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon );
  77. static OSErr    AddListContents ( ListRef theList, void* refCon );
  78.  
  79.  
  80. OSErr            PatchListLDEF ( ListRef theList );
  81.  
  82.  
  83.  
  84.  
  85.  
  86. //
  87. // This is called to create the application's window.
  88. //
  89. void CreateWindow ( void )
  90. {
  91.     OSErr        theErr;
  92.     WindowRef    theWindow;
  93.     
  94.     theErr = CreateListWindow ( &theWindow, AddListContents, nil );
  95.     if ( theErr )
  96.         AlertUser ( kGenericErrorStr, theErr, "\p" );
  97.  
  98.     gWindow = theWindow;
  99.     
  100.     return;
  101. }
  102.  
  103.  
  104.  
  105. //
  106. // This will close the application's window, dispose of any storage we've hung
  107. // off the window, and then dispose of the window itself.
  108. //
  109. WindowRef DestroyWindow ( WindowRef windowRef )
  110. {
  111.     if ( windowRef )
  112.     {
  113.         ListRef theList;
  114.         
  115.         theList = (ListRef) GetWRefCon ( windowRef );
  116.         if ( theList )
  117.             LDispose ( theList );
  118.         
  119.         DisposeWindow ( windowRef );
  120.         
  121.     }
  122.     
  123.     return nil;
  124. }
  125.  
  126.  
  127.  
  128. //
  129. // Creates a window with a list in the centre. contentsProc is a routine
  130. // pointer to intitialize the list. It's passed the refCon to assist.
  131. //
  132. OSErr CreateListWindow ( WindowRef* windowRef, tContentsProcPtr contentsProc, void* refCon )
  133. {
  134.     OSErr        theErr;
  135.     WindowRef    theWindow;
  136.     
  137.     
  138.     theWindow = GetNewCWindow ( kDisplayWindow, nil, (WindowRef) -1 );
  139.     if ( theWindow == nil )
  140.         return (ResError ( )) ? ResError ( ) : resNotFound;
  141.     
  142.     SetPortWindowPort ( theWindow );
  143.     TextFont ( geneva );
  144.     TextFace ( normal );
  145.     TextSize ( 9 );
  146.     
  147.      theErr = CreateContentList ( theWindow, contentsProc, refCon );
  148.     if ( theErr )    goto CleanupAndBail;
  149.     
  150.     SelectWindow ( theWindow );
  151.     ShowWindow ( theWindow );
  152.     
  153.     *windowRef = theWindow;
  154.     
  155.     return noErr;
  156.     
  157.  
  158. CleanupAndBail:
  159.     
  160.     // Don't forget to free any storage we've used so far
  161.     DestroyWindow ( theWindow );
  162.     
  163.     return theErr;
  164. }
  165.  
  166.  
  167.  
  168. //
  169. // This is passed into the CreateDocumentWindow routine
  170. // to add the contents to the newly created list.
  171. //
  172. static OSErr AddListContents ( ListRef theList, void* refCon )
  173. {
  174. #ifdef __MWERKS__
  175.     #pragma unused( theList, refCon )
  176. #endif
  177. /*
  178.     AddToList ( theList, "\pApple Extras\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  179.     AddToList ( theList, "\pApplications\t-\tfolder\tWed, Dec 18, 1995, 12:04 pm" );
  180.     AddToList ( theList, "\pClean System Folder\t-\tfolder\tThu, Dec 7, 1995, 1:46 pm" );
  181.     AddToList ( theList, "\pCommunications\t-\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  182.     AddToList ( theList, "\pCopland Stuff\t-\tfolder\tTue, Aug 29, 1995, 1:14 pm" );
  183.     AddToList ( theList, "\pDevelopment\t-\tfolder\tWed, Dec 13, 1995, 4:27 pm" );
  184.     AddToList ( theList, "\pDocumentation\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  185.     AddToList ( theList, "\pDTS\t-\tfolder\tWed, Dec 18, 1995, 1:07 pm" );
  186.     AddToList ( theList, "\pMisc\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  187.     AddToList ( theList, "\pProjects\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  188.     AddToList ( theList, "\pStress\t-\tfolder\tTue, Aug 29, 1995, 1:14 pm" );
  189.     AddToList ( theList, "\pSystem Folder\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  190.     AddToList ( theList, "\pSystem Installers\t-\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  191.     AddToList ( theList, "\pSystem Picker\t-\tapplication program\tMon, Dec 16, 1995, 8:56 pm" );
  192.     AddToList ( theList, "\pTools\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  193.     AddToList ( theList, "\pAppleScript™ Utilities\t-\tfolder\tThu, Dec 7, 1995, 12:46 pm" );
  194.     AddToList ( theList, "\pDebuggers\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  195.     AddToList ( theList, "\pDebugging Modern Memory Manager\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  196.     AddToList ( theList, "\pDebugging OD Part Editors\t33K\tSimpleText document\tThu, Dec 19, 1995, 12:07 pm" );
  197.     AddToList ( theList, "\pDocumentation Viewers\t-\tfolder\tMon, Dec 16, 1995, 12:56 pm" );
  198.     AddToList ( theList, "\pPartMaker 4.3\t-\tfolder\tMon, Dec 16, 1995, 8:56 pm" );
  199.     AddToList ( theList, "\pReference\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  200.     AddToList ( theList, "\pResource Editors\t5,049K\tfolder\tWed, Dec 6, 1995, 2:21 pm" );
  201.     AddToList ( theList, "\pSDK\t-\tfolder\tThu, Dec 19, 1995, 12:07 pm" );
  202.     AddToList ( theList, "\pSoundMgr3.2Dev.sea\t198K\tapplication program\tThu, Dec 19, 1995, 12:07 pm" );
  203. */    
  204.     
  205.     return noErr;
  206.     
  207. } // AddContents
  208.  
  209.  
  210. // Add the string and scroll down
  211.  
  212. void AddToListContent ( StringPtr pStr )
  213. {
  214.     ListRef        aList;
  215.  
  216.     if ( ! gWindow )
  217.         return;
  218.         
  219.     aList = (ListRef) GetWRefCon ( gWindow );
  220.  
  221.     AddToList ( aList, pStr );
  222.     LScroll ( 0, 1000, aList );        // Scroll down to item
  223.  
  224. } // AddToListContent
  225.  
  226. //
  227. // This is called to create the List Manager list, and initialize it.
  228. //
  229. static OSErr CreateContentList ( WindowRef theWindow, tContentsProcPtr contentsProc, void* refCon )
  230. {
  231.     OSErr        theErr;
  232.     ListHandle    theList;
  233.     Point        cellSize = {0, 0};
  234.     Cell        firstCell = {0, 0};
  235.     Rect         dataRect = {0, 0, 0, 1};
  236.     Rect        viewRect;
  237.     
  238.     
  239.     // The width of a cell needs to be 32767 so any hiliting
  240.     // doesn't stop short of the window width after it's been resized.
  241.     
  242.     viewRect = theWindow->portRect;
  243.     InsetRect ( &viewRect, 20, 20 );
  244.     viewRect.right -= 15;                // minus scrollbar width
  245.     theList = LNew ( &viewRect, &dataRect, cellSize, kLDEFID, theWindow, 
  246.                         false, false, false, true );
  247.  
  248.     #ifndef USE_LDEF
  249.         theErr = PatchListLDEF ( theList );
  250.         if ( theErr )
  251.             return theErr;
  252.     #endif
  253.     
  254.     if ( theList )
  255.     {
  256.         static short    theOffsets[] = { 4, 0, 165, 230, 285 };
  257.         
  258.         (*theList)->refCon = (long) &theOffsets;
  259.         (*theList)->selFlags = lOnlyOne;
  260.         SetWRefCon ( theWindow, (long) theList );
  261.         
  262.         // Since the calling routine is always in the same architecture type as
  263.         // the callback routine, we don't need to worry about any Mixed Mode
  264.         // complications. We just treat it as a straight forward routine pointer.
  265.         if ( contentsProc )
  266.         {
  267.             theErr = (*contentsProc) ( theList, refCon );
  268.             if ( theErr )
  269.                 return theErr;
  270.         }
  271.         
  272.         LSetSelect ( true, firstCell, theList );
  273.         
  274.         // Now the list has been fully prepared, turn the drawing mode on
  275.         LSetDrawingMode ( true, theList );
  276.     }
  277.     
  278.     return noErr;
  279.     
  280. } // CreateContentList
  281.  
  282.  
  283.  
  284. void DoActivate ( EventRecord* theEvent )
  285. {
  286.     Boolean        bActiveFlag = theEvent->modifiers & resumeFlag;
  287.     GrafPtr        savePort;
  288.     WindowRef    theWindow = (WindowRef) theEvent->message;
  289.     
  290.     
  291.     GetPort ( &savePort );
  292.     SetPortWindowPort ( theWindow );
  293.     LActivate ( bActiveFlag, (ListRef) GetWRefCon ( theWindow ) );
  294.     SetPort ( savePort );
  295.     
  296.     return;
  297. }
  298.  
  299.  
  300.  
  301. void DoUpdate ( EventRecord* theEvent )
  302. {
  303.     GrafPtr            savePort;
  304.     CGrafPtr        thePort;
  305.     WindowRef        theWindow = (WindowRef) theEvent->message;
  306.     ListRef            theList = nil;
  307.     Rect            theRect;
  308.     
  309.     
  310.     
  311.     thePort = GetWindowPort ( theWindow );
  312.     
  313.     GetPort ( &savePort );
  314.     SetPortWindowPort ( theWindow );
  315.     BeginUpdate ( theWindow );                    // visRgn temporarily = updateRgn
  316.     EraseRect ( &thePort->portRect );
  317.     
  318.     theList = (ListRef) GetWRefCon ( theWindow );
  319.     if ( theList )
  320.     {
  321.         PenNormal ( );
  322.         GetListRect ( theList, &theRect );
  323.         InsetRect ( &theRect, -1, -1 );
  324.         FrameRect ( &theRect );
  325.         
  326.         LUpdate ( thePort->visRgn, theList );
  327.     }
  328.     
  329.     EndUpdate ( theWindow );                    // restore normal visRgn of grafport
  330.     SetPort ( savePort );
  331.     
  332.     return;
  333. }
  334.  
  335.  
  336.  
  337. void DoContentClick ( WindowRef theWindow, EventRecord* theEvent )
  338. {
  339.     WindowRef    frontWindow;
  340.     
  341.     // If a movable modal is active, ignore click in an inactive 
  342.     // window, otherwise select it or handle the content click.
  343.     
  344.     frontWindow = FrontWindow ( );
  345.     if ( theWindow != frontWindow )
  346.     {
  347.         if ( IsMovableModal ( frontWindow ) )
  348.             SysBeep ( 30 );
  349.         else
  350.             SelectWindow ( theWindow );
  351.     }
  352.     else
  353.     {
  354.         GrafPtr        savePort;
  355.         
  356.         GetPort ( &savePort );
  357.         SetPortWindowPort ( theWindow );
  358.         HandleListClick ( theWindow, theEvent );
  359.         SetPort ( savePort );
  360.     }
  361.     
  362.     return;
  363.     
  364. } // DoContentClick
  365.  
  366.  
  367.  
  368. void DoDragWindow ( WindowRef theWindow, EventRecord* theEvent )
  369. {
  370.     WindowRef    frontWindow;
  371.     
  372.     
  373.     // If a movable modal is active, ignore click in an inactive 
  374.     // title bar, otherwise let the Window Manager handle it.
  375.     
  376.     frontWindow = FrontWindow ( );
  377.     if ( theWindow != frontWindow && IsMovableModal ( frontWindow ) )
  378.         SysBeep ( 30 );
  379.     else                                
  380.     {
  381.         RgnHandle    theRgn;
  382.         Rect        dragRect;
  383.         
  384.         theRgn = GetGrayRgn ( );
  385.         dragRect = (*theRgn)->rgnBBox;
  386.         DragWindow ( theWindow, theEvent->where, &dragRect );
  387.     }
  388.     
  389.     return;
  390. }
  391.  
  392.  
  393.  
  394.  
  395.  
  396.